package bb.lanxing.util.jsbridge;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.webkit.ValueCallback;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.google.gson.Gson;


public class BridgeWebView extends WebView implements WebViewJavascriptBridge, IWebView {
    private BridgeHelper mBridgeHelper;

    public BridgeWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BridgeWebView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public BridgeWebView(Context context) {
        super(context);
        init();
    }

    private void init() {
        clearCache(true);
        WebSettings settings = getSettings();
        settings.setUseWideViewPort(true);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        int i = Build.VERSION.SDK_INT;
        this.mBridgeHelper = new BridgeHelper(this);
    }

    public final BridgeHelper getBridgeHelper() {
        return this.mBridgeHelper;
    }

    public void setGson(Gson gson) {
        this.mBridgeHelper.setGson(gson);
    }

    public boolean isJSLoaded() {
        return this.mBridgeHelper.isJSLoaded();
    }

    @Override
    public void setWebViewClient(WebViewClient client) {
        if (client instanceof BridgeWebViewClient) {
            super.setWebViewClient(client);
            return;
        }
        Log.w("BridgeWebView", "this client is deprecated, you should use BridgeWebViewClient");
        super.setWebViewClient(new WebViewClientProxy(this.mBridgeHelper, client));
    }

    @Override
    public void sendToWeb(Object data) {
        this.mBridgeHelper.sendToWeb(data);
    }

    @Override
    public void sendToWeb(Object data, CallBackFunction responseCallback) {
        this.mBridgeHelper.sendToWeb(data, responseCallback);
    }

    public void callHandler(String handlerName, String data, CallBackFunction callBack) {
        this.mBridgeHelper.callHandler(handlerName, data, callBack);
    }

    public void setDefaultHandler(BridgeHandler handler) {
        this.mBridgeHelper.setDefaultHandler(handler);
    }

    public void registerHandler(String handlerName, BridgeHandler handler) {
        this.mBridgeHelper.registerHandler(handlerName, handler);
    }

    public void unregisterHandler(String handlerName) {
        this.mBridgeHelper.unregisterHandler(handlerName);
    }

    @Override
    public void sendToWeb(String function, Object... values) {
        this.mBridgeHelper.sendToWeb(function, values);
    }

    @Override
    public void sendToWeb(String function, ValueCallback<String> callback, Object... values) {
        this.mBridgeHelper.sendToWeb(function, callback, values);
    }

    @Override
    public void destroy() {
        super.destroy();
        this.mBridgeHelper.destroy();
    }
}
